from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from app.core.database import get_db
from app.crud.history import history
from app.schemas.deepseek import (
    PromptRequest,
    PromptResponse,
    ChatRequest,
    ChatResponse,
    ResponseData
)
from app.services.deepseek import DeepSeekService
from loguru import logger

router = APIRouter()

@router.post("/prompt", response_model=PromptResponse)
async def generate_prompt(
    request: PromptRequest,
    db: Session = Depends(get_db)
):
    """生成提示词"""
    try:
        # 调用DeepSeek API生成提示词
        service = DeepSeekService()
        response = await service.generate_prompt(request)
        
        # 记录历史
        history.create_prompt_history(
            db,
            input_text=request.input_text,
            output_text=response.data.output_text,
            parameters=request.model_dump()
        )
        
        return response
    except Exception as e:
        logger.error(f"生成提示词失败: {str(e)}")
        raise HTTPException(
            status_code=500, 
            detail=f"生成提示词失败: {str(e)}"
        )

@router.post("/chat", response_model=ChatResponse)
async def chat(
    request: ChatRequest,
    db: Session = Depends(get_db)
):
    """对话"""
    try:
        # 调用DeepSeek API进行对话
        service = DeepSeekService()
        response = await service.chat(request)
        
        # 记录历史
        history.create_chat_history(
            db,
            input_text=request.input_text,
            output_text=response.data.output_text,
            parameters=request.model_dump()
        )
        
        return response
    except Exception as e:
        logger.error(f"对话失败: {str(e)}")
        raise HTTPException(
            status_code=500, 
            detail=f"对话失败: {str(e)}"
        ) 